package demo.game.chess.client.ui;

import java.util.ArrayList;
import java.util.List;

import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.MouseMoveEvent;
import com.google.gwt.event.dom.client.MouseMoveHandler;
import com.google.gwt.event.dom.client.MouseOutEvent;
import com.google.gwt.event.dom.client.MouseOutHandler;
import com.google.gwt.event.dom.client.MouseOverEvent;
import com.google.gwt.event.dom.client.MouseOverHandler;
import com.google.gwt.user.client.ui.FocusPanel;
import com.google.gwt.user.client.ui.SimplePanel;

import demo.game.chess.client.ui.AbstractColorTheme.EnumColorStyle;
import demo.game.chess.common.ChessCell;

public class ChessCellUI extends FocusPanel {

	private final ChessBoardUI boardUI;
	private final ChessCell cell;

	private ChessPieceUI piece;

	private final SimplePanel innerPanel = new SimplePanel();

	private final List<EnumColorStyle> highlightStyles = new ArrayList<EnumColorStyle>();

	public ChessCellUI(final ChessBoardUI boardUI, final ChessCell cell) {
		super();
		this.boardUI = boardUI;
		this.cell = cell;

		this.setWidget(this.innerPanel);

		this.addMouseOverHandler(new MouseOverHandler() {

			@Override
			public void onMouseOver(final MouseOverEvent event) {

				if (!boardUI.getMoveValidator().isDndMode()) {
					boardUI.getMoveValidator().onMouseIn(ChessCellUI.this);
				}
			}
		});

		this.addMouseMoveHandler(new MouseMoveHandler() {

			@Override
			public void onMouseMove(final MouseMoveEvent event) {
				if (!boardUI.getMoveValidator().isDndMode()) {
					boardUI.getMoveValidator().onMouseIn(ChessCellUI.this);
				}
			}
		});

		this.addMouseOutHandler(new MouseOutHandler() {

			@Override
			public void onMouseOut(final MouseOutEvent event) {
				if (!boardUI.getMoveValidator().isDndMode()) {
					boardUI.getMoveValidator().onMouseOut(ChessCellUI.this);
				}
			}
		});

		this.addClickHandler(new ClickHandler() {

			@Override
			public void onClick(final ClickEvent event) {

				//Log.debug(ChessCellUI.this.getClass() + " onClick cell=" +cell);

				boardUI.getMoveValidator().onDropPieceAt(ChessCellUI.this);

			}
		});

	}

	public void addHighlightStyle(final EnumColorStyle style) {
		this.addStyleName(this.boardUI.getColorTheme().getColorStyle(style));
		this.highlightStyles.add(style);
	}

	public void clearChessPiece() {
		this.piece = null;
		this.innerPanel.setWidget(null);
	}

	public void clearHighlightStyles() {
		for (EnumColorStyle s : this.highlightStyles) {
			this.removeStyleName(this.boardUI.getColorTheme().getColorStyle(s));
		}
		this.highlightStyles.clear();
	}

	public ChessCell getCell() {
		return this.cell;
	}

	public ChessPieceUI getChessPiece() {
		return this.piece;
	}

	public void removeHighlightStyle(final EnumColorStyle style) {
		this.removeStyleName(this.boardUI.getColorTheme().getColorStyle(style));
		this.highlightStyles.remove(style);
	}

	public void setChessPiece(final ChessPieceUI pieceUI) {
		this.piece = pieceUI;
		this.innerPanel.setWidget(this.piece);
	}

	@Override
	public String toString() {
		return this.cell.toString() + "_" + this.piece;
	}

	public void updateTheme() {

		setStyleName("chessCell");
		this.innerPanel.setStyleName("chessCellInnerPanel");
		if (this.cell.isBlack()) {
			addStyleName(this.boardUI.getColorTheme().getColorStyle(
					EnumColorStyle.blackCell));
			this.innerPanel.addStyleName(this.boardUI.getColorTheme().getColorStyle(
					EnumColorStyle.blackCell));
		} else {
			addStyleName(this.boardUI.getColorTheme().getColorStyle(
					EnumColorStyle.whiteCell));
			this.innerPanel.addStyleName(this.boardUI.getColorTheme().getColorStyle(
					EnumColorStyle.whiteCell));
		}

		for (EnumColorStyle s : this.highlightStyles) {
			addHighlightStyle(s);
		}

	}

}
